
#include <algorithm>
#include <iostream>
#include <sys/time.h>
#include <unordered_map>
#include <vector>

using namespace std;

bool containsNearbyDuplicate(vector<int>& nums, int k) {
  if(0 >= k){
    return false;
  }

  //convert the nums to an unordered_map with the nums's value as the map' key
  unordered_map<int,int> umap;
  for(int i=0; i<nums.size(); ++i){
    int umap_key = nums.at(i);
    int umap_value = i;
    auto it = umap.find(umap_key); 

    if(it == umap.end() ){  
      umap[umap_key] = umap_value;
    }
    else{
      if(i - it->second <= k )  //if find two same value, check their difference 
        return true;
      else {  //if their difference unstatify the condition then update the the umap with the newer num
        umap.erase(it);
        umap[umap_key] = umap_value;
      }

    }

  }

  return false;

}



int main(int argc,char *argv[]){

  struct  timeval    tv1,tv2;
  struct  timezone   tz;
  int time1 = gettimeofday(&tv1,&tz);

  //func();

  int time2 = gettimeofday(&tv2,&tz);
  cout << "time consuming(local machine/leetcode server):"<<endl
    <<"leetcode server time consuming should be less than 500ms"<<endl
    << tv2.tv_usec - tv1.tv_usec <<"us/"<<0.11*( tv2.tv_usec - tv1.tv_usec )<<"ms"<< endl;



  return 0;
}
